<?php
/*
|--------------------------------------------------------------------------
| 项目根目录
|--------------------------------------------------------------------------
*/
define('ROOT_PATH', dirname(__DIR__) . '/');
/*
|--------------------------------------------------------------------------
| 是否是CLI模式
|--------------------------------------------------------------------------
*/
define('IS_CLI', PHP_SAPI == 'cli');
/*
|--------------------------------------------------------------------------
| 开发环境
|--------------------------------------------------------------------------
*/
define('ENV_FILE', '.env');


require_once __DIR__ . '/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__ . '/../', ENV_FILE))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

/*
|--------------------------------------------------------------------------
| HTTP或者HTTPS
|--------------------------------------------------------------------------
*/
if (is_https()) {
    define('PROTOCAL', 'https:');
} else {
    define('PROTOCAL', 'http:');
}

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new App\Application(
    realpath(__DIR__ . '/../')
);

$app->withFacades();

$app->withEloquent();
/*
|--------------------------------------------------------------------------
| 加载应用配置文件
|--------------------------------------------------------------------------
*/
$app->configure('app');
/*
|--------------------------------------------------------------------------
| 加载数据库配置文件
|--------------------------------------------------------------------------
*/
$app->configure('database');
/*
|--------------------------------------------------------------------------
| 加载缓存配置文件
|--------------------------------------------------------------------------
*/
$app->configure('redis');
/*
|--------------------------------------------------------------------------
| 加载路由配置文件
|--------------------------------------------------------------------------
*/
//$app->configure('route');



/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);


/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

$app->middleware([
    App\Http\Middleware\AfterResponseMiddleware::class,
    App\Http\Middleware\LoginMiddleware::class
]);

$app->routeMiddleware([
    'user' => App\Http\Middleware\UserMiddleware::class,
    'Wpt' => App\Http\Middleware\AfterMiddleware::class,
    'Student' => App\Http\Middleware\AfterMiddleware::class,
     'Test' => App\Http\Middleware\AfterMiddleware::class,
    // 'Demo' => App\Http\Middleware\AfterMiddleware::class,
    // 'cgg' => App\Http\Middleware\AfterMiddleware::class,
    // 'G' => App\Http\Middleware\AfterMiddleware::class,
    'Teacher' => App\Http\Middleware\AfterMiddleware::class,
    //'Exam' => App\Http\Middleware\AfterMiddleware::class,

    // 默认异常处理 所有路由都加载此中间件
    'exception' => \App\Http\Middleware\AfterMiddleware::class,
    // 跨域中间件
    'cross' => App\Http\Middleware\CrossRequestMiddleware::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);

//business
$app->register(App\Providers\BusinessServiceProvider::class);

$app->register(App\Providers\AuthServiceProvider::class);//接口认证服务
//business
$app->register(App\Providers\BusinessServiceProvider::class);
//options
$app->register(App\Providers\CatchAllOptionsRequestsProvider::class);

/*
 *
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

/**
 * 日志按天生成  0 为永久保留  其他表示保留多少天  7 保留最近7天的
 */
$app->configureMonologUsing(function (Monolog\Logger $monoLog) use ($app) {

    $url = \Illuminate\Support\Facades\Request::fullUrl();
    $extraInfo = "api : %s\nRequest : %s\nCookie : %s\nReferer : %s\nHTTP_USER_AGENT : %s\nUser : %s\n\n\n";
    $extraInfo = sprintf(
        $extraInfo,
        $url,
        var_export(\Illuminate\Support\Facades\Request::all(), true),
        var_export(\Illuminate\Support\Facades\Cookie::get(), true),
        $_SERVER['HTTP_REFERER'] ?? '',
        $_SERVER['HTTP_USER_AGENT'] ?? '',
        var_export((array)app('DefaultUserinfo'), true)
    );

    $output = "[%datetime%] %channel%.%level_name%: %message%\n" . $extraInfo;
    $formatter = new \Monolog\Formatter\LineFormatter($output, null, true);
    return $monoLog->pushHandler(
        (new \Monolog\Handler\RotatingFileHandler($app->storagePath() . '/logs/lumen.log', 0))->setFormatter($formatter)
    );
});
// 注册路由
$app->registerRouter();
return $app;
